﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Configuration.Provider;
using System.Web.Profile;

namespace newtelligence.DasBlog.Web.Core
{
    public class SiteSecurityRoleProvider : RoleProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            bool makeAdmin=false;
            bool makeContrib=false;

            foreach (string role in roleNames)
            {
                if (string.Compare(role, "admin", true) == 0)
                    makeAdmin = true;
                else if (string.Compare(role, "contributor", true) == 0)
                    makeContrib = true;
                else
                    throw new ProviderException();
            }

            foreach (string username in usernames)
            {
                User user = SiteSecurity.GetUser(username);
                if (user != null)
                {
                    if (makeAdmin)
                        user.Role = "admin";
                    else if (makeContrib)
                        user.Role = "contributor";
                    else
                        continue;

                    SiteSecurity.UpdateUser(user);
                }
                else
                {
                    throw new ProviderException();
                }
            }
        }

        public override string ApplicationName
        {
            get;
            set;
        }

        public override void CreateRole(string roleName)
        {
            throw new ProviderException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new ProviderException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            List<string> users = new List<string>();
            foreach (User user in SiteSecurity.GetUsersInRole(roleName))
            {
                if (usernameToMatch == null || user.Name == usernameToMatch)
                {
                    users.Add(user.Name);
                }
            }
            return users.ToArray();
        }

        public override string[] GetAllRoles()
        {
            return new string[] { "admin", "contributor" };
        }

        public override string[] GetRolesForUser(string username)
        {
            User user = SiteSecurity.GetUser(username);
            if (user != null)
            {
                if (string.Compare(user.Role, "admin", true) == 0)
                    return new string[] { user.Role, "contributor" };
                else
                    return new string[] { user.Role };
            }
            else
            {
                throw new ProviderException();
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            List<string> users = new List<string>();
            foreach (User user in SiteSecurity.GetUsersInRole(roleName))
            {
                users.Add(user.Name);
            }
            return users.ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            User user = SiteSecurity.GetUser(username);
            if (user != null)
            {
                return string.Compare(user.Role, roleName, true) == 0;
            }
            else
            {
                throw new ProviderException();
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            bool removeAdmin=false;
            bool removeContrib=false;

            foreach (string role in roleNames)
            {
                if (string.Compare(role, "admin", true) == 0)
                    removeAdmin = true;
                else if (string.Compare(role, "contributor", true) == 0)
                    removeContrib = true;
                else
                    throw new ProviderException();
            }
                    

            foreach (string username in usernames)
            {
                User user = SiteSecurity.GetUser(username);
                if (user != null)
                {
                    if (removeAdmin && user.Role == "admin")
                        user.Role = "contributor";
                    else if (removeContrib && user.Role == "contributor")
                        user.Active = false;

                    SiteSecurity.UpdateUser(user);
                }
                else
                {
                    throw new ProviderException();
                }
            }
        }

        public override bool RoleExists(string roleName)
        {
            return roleName == "admin" || roleName == "contributor";
        }
    }
}
